#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#define PUBLIC_FIFO "MYFIFO"
#define PRIVATE2 "P_FO2"
#define PRIVATE2_HEADER "private 2 > "

int main(int argc,char *argv[]){
    int fd;
    int fd2;
    char buf[120];
    char buf2[120]=PRIVATE2_HEADER;
    char num[100]={0};

    //fd=open(PUBLIC_FIFO,O_WRONLY);
    fd2=open(PRIVATE2, O_RDONLY);
    if(read(fd2, buf, 120)!=-1){
        printf("%s\n", buf);
        close(fd2);
    }
    //printf("Read success!\n");

    while(1){
        //printf("Open PUBLIC_FIFO\n");
        if((fd=open(PUBLIC_FIFO,O_WRONLY))==-1){
            printf("open failed\n" );
            exit(1);
        }
        printf("Please input : ");
        fgets(num, 100, stdin);
        if(strncmp(num, "end", 3)==0){
            break;
        }
        strcpy(buf2, PRIVATE2_HEADER);
        strcat(buf2, num);
        if(write(fd, buf2, 220) > 0){
            printf("Write Data Success!\n");
            close(fd);
        }

        fd2=open(PRIVATE2, O_RDONLY);
        if(read(fd2, buf, 120)!=-1){
            printf("%s\n\n", buf);
            close(fd2);
        }
    }
    close(fd);
    return 0;
}
